<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="searchForm">
<!--        <el-form-item>-->
<!--          <el-input v-model="searchForm.name" placeholder="宠物名称"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item>-->
<!--          <el-button type="primary" v-on:click="getPetTypes">查询</el-button>-->
<!--        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table
        :data="tableData"
        style="width: 100%"
        highlight-current-row v-loading="listLoading"
        @selection-change="selsChange"
        row-key="id"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column
          prop="name"
          label="种类">
      </el-table-column>
      <el-table-column
          prop="description"
          label="描述">
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑 新增 界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="种类" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="saleprice">
          <el-input v-model="editForm.description" auto-complete="off"></el-input>
        </el-form-item>
        <span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;宠物种类</span>
        <el-select v-model="editForm.pid" placeholder="请选择">
          <el-option
              v-for="item in tableData"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; //调用编辑器
import 'quill/dist/quill.core.css';
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from '../../common/js/quill-config.js'

// components：定义局部组件关键字
export default {
  components: {
    quillEditor
  },
  data() {
    return {
      /*假数据*/
      tableData: [],
      /*假数据*/

      quillOption: quillConfig,
      fileList: [],
      title: '',
      searchForm: {
        name: ''
      },
      pets: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      editForm: {
        id: 0,
        name: '',
        description:'',
        pid:0,
      },
      petTypes: null,
      //
      // intro:'',
      // adoptNotice: '',
      //
      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      }

    }
  },
  methods: {
    /*树形菜单，假方法*/
    handleCurrentChange(val) {
      this.page = val;
      this.getPetTypes();
    },
    //获取用户列表

    // 获取宠物类型树数据
    getPetTypes() {
      let keyword = this.searchForm.name
      this.$http.get("/petType/tree").then(res => {
        this.tableData = res.data;
        console.debug(res);
      });
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //开启加载的模态框
        this.listLoading = true;
        //NProgress.start();
        console.debug(row)
        //后台地址的请求
        this.$http.delete("/petType/" + row.id).then((res) => {
          this.listLoading = false;
          console.debug(res);
          if (res.data.success) {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }

          this.getPetTypes();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      //打开弹出框
      this.editFormVisible = true;
      //回显
      console.debug(row)
      console.debug(this.editForm)
      this.editForm = Object.assign({}, row);
      console.log(row)
    },
    //显示新增界面
    handleAdd: function () {
      //打开弹出框
      this.editFormVisible = true;
      this.title = '新增种类';
      //初始化表单字段-利用的双向绑定的特性
      this.editForm = {
        name: '',
        description:'',
        pid:0,
      };
    },
    //编辑
    editSubmit: function () {
      //验证当前的表单是否符合验证规则
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            let para = Object.assign({}, this.editForm);
            this.$http.put('/petType', para).then((res) => {
              this.editLoading = false;
              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              //重置表单-调用现成的方法
              this.$refs['editForm'].resetFields();
              //关闭 加载框
              this.editFormVisible = false;
              //重新刷新页面数据
              this.getPetTypes();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      console.log(sels);
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {ids: ids};
        this.$http.post('/petType/dels', ids)
        .then((res) => {
          res = res.data
          if(res.success){
            this.listLoading = false;
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }
          this.getPetTypes();
          });
      }).catch(() => {

      });
    }
  },
  mounted() {
    //当页面加载完以后。执行的方法
    this.getPetTypes();
  }
}

</script>

<style scoped>

</style>